/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import pojo.Khachhang;
import util.HibernateUtil;

/**
 *
 * @author Katori
 */
public class KhachHangDAO {
        public static List<Khachhang> layTatCaKhachhang() {
        List<Khachhang> dsKhachHang = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.getCurrentSession();
        Transaction trans = ss.getTransaction();
        trans.begin();
        try {
            String hql = "from pojo.Khachhang";
            dsKhachHang = ss.createQuery(hql).list();
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
        return dsKhachHang;
    }

    public static Khachhang layKhachHang(String tenTK) {
        Khachhang kh = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.getCurrentSession();
        Transaction trans = ss.getTransaction();
        trans.begin();
        try {
            kh = (Khachhang) ss.get(Khachhang.class, tenTK);
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
        return kh;
    }

    public static boolean themKhachHang(Khachhang kh) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(kh);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(e);
            return false;
        } finally {
            session.close();
        }
    }

    public static boolean capNhatKhachHang(Khachhang kh) {
        if (KhachHangDAO.layKhachHang(kh.getTenTaiKhoan()) == null) {
            return false;
        }

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.getTransaction().begin();
            session.update(kh);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.print(e);
            return false;
        }
    }

    public static boolean xoaKhachHang(String tenTK) {
        if (KhachHangDAO.layKhachHang(tenTK) == null) {
            return false;
        }

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.getTransaction().begin();
            String hql = "update pojo.Khachhang set BiXoa = :xoa where TenTaiKhoan = :userName";
            Query query = session.createQuery(hql);
            query.setString("xoa", "1");
            query.setString("userName", tenTK);
            int rowCount = query.executeUpdate();
            session.getTransaction().commit();
            
            if (rowCount > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.print(e);
            return false;
        }
    }
}
